package Work.Cope;

import java.io.*;
import java.util.Scanner;
public class TestCopy {
  /** Main method
     @param args[0] for sourcefile 
     @param args[1] for target file
   */
  public static void main(String[] args) throws IOException { 

    Scanner san = new Scanner(System.in);
    System.out.println("请输入源文件名：");
    String SourceFile = san.next();
    System.out.println("请输入输出文件名：");
    String OutputFile = san.next();

    // Check command-line parameter usage (检查命令行参数使用情况)
    if (SourceFile.isEmpty()||OutputFile.isEmpty()) {
      System.out.println(
        "Usage: java Copy sourceFile targetfile");
      System.exit(1);
    }

    // Check if source file exists (检查源文件是否存在)
    File sourceFile = new File(SourceFile);
    if (!sourceFile.exists()) {
       System.out.println("Source file " + SourceFile
         + " does not exist");
       System.exit(2);
    }

    // Check if target file exists (检查目标文件是否存在)
    File outputFile = new File(OutputFile);
    if (outputFile.exists()) {
      System.out.println("Target file " + OutputFile
        + " already exists");
      System.exit(3);
    }

    try (
      // Create an input stream (创建输入流)
      BufferedInputStream input = 
        new BufferedInputStream(new FileInputStream(sourceFile));
  
      // Create an output stream (创建一个输出流)
      BufferedOutputStream output = 
        new BufferedOutputStream(new FileOutputStream(outputFile))
    ) {
      // Continuously read a byte from input and write it to output (连续地从输入端读取一个字节并将其写入输出端)
      int r, numberOfBytesCopied = 0;
      while ((r = input.read()) != -1) {
        output.write((byte)(r-5));
        numberOfBytesCopied++;
      }

      // Display the file size (显示文件大小)
      System.out.println(numberOfBytesCopied + " bytes copied");

//      加密解密
      Scanner txt = new Scanner(System.in);
      System.out.println("请输入加密文件所在路径：");
      SourceFile = txt.next();

      int n,i = 0;
      try (FileInputStream put = new FileInputStream(sourceFile)){
        byte[] str = new byte[put.available()];
        while ((n = put.read()) != -1){
          str[i] = (byte) (n+5);
          i++;
        }
        System.out.println("解压成功！文件内容为：");
        System.out.println(new String(str));
      }
    }
  }

}
